function p = primes_b(n)
% primes_b  - generate list of prime numbers
%
% See also primes

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin ~= 1 || ...
    numel(n) ~= 1 || ...
   (~isa(n, 'double') && ...
    ~isa(n, 'single')) || ...
    isinf(n) || ...
    isnan(n) || ...
    n < 1
    error( ...
        'BVQXtools:BadArgument', ...
        'Bad argument.' ...
    );
end
n = ceil(n);
if n < 2
    p = reshape(n([]), [1, 0]);
    return;
end
p = true(1, floor((n+1) / 2));
p(1) = false;
q = numel(p);
m = ceil(sqrt(n) / 2);
for k = 2:m
    if p(k)
        k2 = 2 * k - 1;
        p(k+k2:k2:q) = false;
    end
end
p = [2, 2 * find(p) - 1];
